function measureDistances
% MEASUREDISTANCES  Measures distances between mouse-selected points.
% 
% x and y are vectors of points as obtained using getpts
%
% Data saved to the WS as structure variable 'distanceData'

% gs 2004; 
% modified for use with mapAnalysis, 2008 03 08
% ---------------------------------------------------------

[x,y] = getpts;
distanceData.x = x;
distanceData.y = y;

disp(' ');
disp('measureDistances: X positions of points:');
if max(abs(x))>1
    disp(num2str(round(x')));
else
    disp(num2str(x'));
end

disp(' ');
disp('measureDistances: Y positions of points:');
if max(abs(y))>1
    disp(num2str(round(y')));
else
    disp(num2str(y'));
end

if numel(x)<2
    beep
    disp(' ');
    disp('measureDistances: need to select at least 2 points to measure a distance.');
    disp(' ');
    return
end

for n = 2:size(x,1);
    dx = x(n) - x(n-1);
    dy = y(n) - y(n-1);
    d(n) = sqrt(dx^2 + dy^2);
end
distanceData.d = d;

disp(' ');
disp('measureDistances: distances between points:');
if max(abs(d))>1
    disp(num2str(round(d)));
else
    disp(num2str(d));
end

disp(' ');
disp('measureDistances: cumulative distances between points:');
if max(abs(d))>1
    disp(num2str(round(cumsum(d))));
else
    disp(num2str(cumsum(d)));
end
disp(' ');

% yfrac calculation
if numel(d) == 4
    if max(abs(d))>1
        D = cumsum(d);
        ysoma = D(end-1);
        yWM = D(end);
        yfrac = round(1000*(ysoma/yWM))/1000;
        disp('measureDistances: yfrac:');
        disp(num2str(yfrac));
        
        distanceData.yL2 = D(end-2);
        distanceData.ysoma = ysoma;
        distanceData.yWM = yWM;
        distanceData.yfrac = yfrac;
    else
        disp('measureDistances: cumulative distances between points:');
        disp(num2str(cumsum(d)));
    end
    disp(' ');
end

assignin('base', 'distanceData', distanceData);

